<?php
/*
  Template Name: post_new_blog
 */ 
  ?>
    <!--css here -->
    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/includes/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/includes/js/bootbox.js"></script>

    <style>
    .mceContentBody {
        background-color: white;
    }
    #titlediv{
        width:555px;
    }
    #title{
        width: 100%;
        height: 28px;
        margin-bottom: 20px;
        font-size: 16px;
    }
    #titlewrap{
        width: 555px;
    }
    #postdivrich{
        width: 555px;
    }  
    .button_group{
        margin-left: auto;
        margin-right: auto;
    }
    #buttoncontainer{
        width: 100%;
    }
    #button_area{
        width: 100%;
    }
    #itags{
        width: 550px;
        height: 27px;
        font-size: 16px;

    }

    .btnsubmit {
        font-size: 15px;
        height: 31px;
        margin-left: 186px;
        margin-right: auto;
        margin-top: 20px;
        margin-bottom: 20px;
        width: 161px;
    }

    .tagchecklist span a {
        margin: 5px 0 0 -9px;
        cursor: pointer;
        width: 10px;
        height: 10px;
        display: block;
        float: left;
        text-indent: -9999px;
        overflow: hidden;
        position: absolute;
        background: url(<?php echo get_admin_url(); ?>images/xit.gif) no-repeat -10px 0;
    }
    .tagchecklist {
        font-size: 12px;
        overflow: auto;
    }

    .tagchecklist span {
        cursor: default;
        display: block;
        float: left;
        font-size: 11px;
        line-height: 1.8em;
        margin-right: 25px;
        white-space: nowrap;
    }

    .spanchecktags{
        position: relative;
        margin: 10px;
    }
    
    .valueTag{
        font-size: 14px !important;
        margin-left: 4px !important;
    }
    .post_category:hover{
        background-color: blue;
    }
    .edit_category{
        float: right;
        display: none;
    }
    .selected{
        background-color: #C5C5C5 !important;
    }
    .content_right ul li{
        background-image: none !important;
    }
    .post_category span:hover{
        cursor: default;
    }

    </style>
    
    <script>
        var controlerLink ="<?php echo get_template_directory_uri() . "/controller/category-controller.php" ?>";
    </script>
    <script type="text/javascript" src="<?php echo get_template_directory_uri() . '/includes/js/Utils.js'; ?>"></script>
    <script type="text/javascript" src="<?php echo get_template_directory_uri() . '/includes/js/ys-post.js'; ?>"></script>
    
<?php
global $wpdb, $current_user;
// dump(current_user_can( 'edit_post', 12 ));

// $author = get_the_author(); 
// dump($author);


get_currentuserinfo();

$post_update_id = 0;
if(isset($_REQUEST["post_id"]) && $_REQUEST["post_id"]!=""){
     $post_update_id = $_REQUEST["post_id"]; 
     $post_data = get_post( $post_update_id); 
     if($post_data == null || !isAuthorOfPost($post_data)){
        $post_update_id = 0;    
}

$blog_link = get_permalink( get_page_by_path( 'Blog' ) ) ;
$ad_link = get_permalink( get_page_by_path( 'Home' ) ) ;
//var_dump($ad_link);exit;



}

//echo $post_update_id; exit;

?>

<div class="content user-dashboard">

    <div class="content_botbg">

        <div class="content_res">
      <!-- left block -->
            <div class="content_left">
                <div class="shadowblock_out">

                    <div class="shadowblock">
                       <div class="wrap" id="titlediv">
                        <h2>
                            <?php
                            echo esc_html($title);
                            if (isset($post_new_file) && current_user_can($post_type_object->cap->create_posts))
                                echo ' <a href="' . esc_url($post_new_file) . '" class="add-new-h2">' . esc_html($post_type_object->labels->add_new) . '</a>';
                            ?></h2>
                            <?php if ($notice) : ?>
                            <div id="notice" class="error"><p><?php echo $notice ?></p></div>
                        <?php endif; ?>
                        <?php if ($message) : ?>
                        <div id="message" class="updated"><p><?php echo $message; ?></p></div>
                    <?php endif; ?>
                    <form name="frmpost" action="<?php echo get_template_directory_uri(); ?>/controller/post-controller.php" method="post" id="frmPost">
                        <!-- <form name="frmpost" action="" method="post" id="frmPost"> -->
                        <?php wp_nonce_field($nonce_action); ?>
                        <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_ID ?>" />
                        <input type="hidden"  name="postaction" value="post_new" />
                        <input type="hidden" id="post_author" name="post_author" value="<?php echo esc_attr($post->post_author); ?>" />
                        <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr($post_type) ?>" />
                        <?php 
                        if($post_update_id> 0){
                            $listCategory = get_the_category($post_update_id);
                            $listCategoryArr = array();
                            foreach ($listCategory as $item) {
                                array_push($listCategoryArr,$item->cat_ID);
                            }
                            echo "<input type=\"hidden\" id=\"post_category\" name=\"post_category\" value='".json_encode($listCategoryArr)."' />";
                        }else{
                            echo '<input type="hidden" id="post_category" name="post_category" value="" />';
                        }


                        ?>


                        <?php 
                        if($post_update_id!=0){
                            echo ' <input type="hidden" name="post_id" id="post_id"value="'.$post_update_id.'"/>';
                        }
                        ?>
<!--            <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr($post->post_status) ?>" />
    <input type="hidden" id="referredby" name="referredby" value="<?php echo esc_url(stripslashes(wp_get_referer())); ?>" />-->
    <div id="poststuff">
        <div id="post-body" class="metabox-holder columns">
            <div id="post-body-content">
                <div id="111titlediv">
                    <div id="titlewrap">
                        <!--                                    <label class="screen-reader-text" id="title-prompt-text" for="title"></label>-->
                        <input type="text" name="post_title" size="30"  value="<?php 
                        if($post_update_id!=0){
                            echo $post_data->post_title;
                        }
                        ?>" placeholder="Tên bài viết" id="title" autocomplete="off" />

                    </div>
                    <div class="inside">
                        <?php
//                                    $sample_permalink_html = $post_type_object->public ? get_sample_permalink_html($post->ID) : '';
//                                    $shortlink = wp_get_shortlink($post->ID, 'post');
//                                    if (!empty($shortlink))
//                                        $sample_permalink_html .= '<input id="shortlink" type="hidden" value="' . esc_attr($shortlink) . '" /><a href="#" class="button button-small" onclick="prompt(&#39;URL:&#39;, jQuery(\'#shortlink\').val()); return false;">' . __('Get Shortlink') . '</a>';
                        ?>
                        <div id="edit-slug-box" class="hide-if-no-js">
                            <?php
//                                        if ($sample_permalink_html && 'auto-draft' != $post->post_status)
//                                            echo $sample_permalink_html;
                            ?>
                        </div>
                    </div>

                    <?php
//                                wp_nonce_field('samplepermalink', 'samplepermalinknonce', false);
                    ?>

                </div><!-- /titlediv -->

                <div id="postdivrich" class="postarea">
                    <?php 
                    $contentEditor = "";
                    if($post_update_id==0){
                                            //$contentEditor = "Nội dung bài viết bạn muốn chia sẻ cho mọi người :)";   
                        $contentEditor = "";   
                    } else{
                     $contentEditor = $post_data->post_content ;
                 }
                 wp_editor($contentEditor, 'content', array('dfw' => true, 'tabfocus_elements' => 'sample-permalink,post-preview', 'editor_height' => 400)); ?>
                 <!--                                 <table id="post-status-info" cellspacing="0"><tbody><tr> -->
                 <!--                                            <td id="wp-word-count"><?php printf(__('Word count: %s'), '<span class="word-count">0</span>'); ?></td>-->
                 <!--                                             <td class="autosave-info"> -->
                 <!--                                                 <span class="autosave-message">&nbsp;</span> -->
                 <?php
// //                                                if ('auto-draft' != $post->post_status) {
// //                                                    echo '<span id="last-edit">';
// //                                                    if ($last_id = get_post_meta($post_ID, '_edit_last', true)) {
// //                                                        $last_user = get_userdata($last_id);
// //                                                        printf(__('Last edited by %1$s on %2$s at %3$s'), esc_html($last_user->display_name), mysql2date(get_option('date_format'), $post->post_modified), mysql2date(get_option('time_format'), $post->post_modified));
// //                                                    } else {
// //                                                        printf(__('Last edited on %1$s at %2$s'), mysql2date(get_option('date_format'), $post->post_modified), mysql2date(get_option('time_format'), $post->post_modified));
// //                                                    }
// //                                                    echo '</span>';
// //                                                }
//                                                 ?>
<!--                                             </td> -->
<!--                                         </tr></tbody></table> -->

</div>
</div><!-- /post-body-content -->
</div><!-- /post-body -->
<br class="clear" />
</div><!-- /poststuff -->

<!--tags-->
<div id="tagsdiv-post_tag" class="postbox">
    <div class="inside">
        <div class="tagsdiv" id="post_tag">
            <div class="jaxtag">
                <input type="hidden" name="post_tags" class="the-tags" id="iTagsList" value="" />
                <div class="ajaxtag">
                    <input type="text" name="newtag[post_tag]" class="newtag form-input-tip" size="16" autocomplete="off" value="" id="itags" placeholder="Nhập tag cho bài viết, nhấn enter để thêm tag."/>
                </div>
            </div>
            <div class="tagchecklist" id="listTags">
                <?php
                if($post_update_id!=0){
                    $tags =  $post_data->tags_input ;
                    foreach ($tags as $key => $value) {
                     echo '<span class="spanchecktags"><a class="ntdelbutton">X</a><span class="valueTag">'.$value.'</span></span>';
                 }   
             } 
             ?>
         </div>
     </div>
     <!--                        <p class="tagcloud-link">Hoáº·c <a href="javascript: void(0);" class="tagcloud-link" id="link-post_tag">Chá»�n cÃ¡c tháº» cÃ³ sáºµn</a></p>-->
 </div>
</div>

</form>
<div id="button_area">
    <?php
                  // if (is_user_logged_in()) :
                  //       if (current_user_can('edit_post')) {
                  //             if($post_update_id!=0){
                  //                       $tags =  $post_data->tags_input ;
                  //                       foreach ($tags as $key => $value) {
                  //                              echo '<span class="spanchecktags"><a class="ntdelbutton">X</a><span class="valueTag">'.$value.'</span></span>';
                  //                       }   
                  //              } 
                  //       } 
                  //   endif;
    if($post_update_id> 0){
        echo '<input value="Cập nhật bài viết" type="button" class="btnsubmit" id="btnSubmitNewPost"/> ';
        echo '<a href="javascript:void(0);" id="deletePost">Xóa bài viết</a>';
    }else{
        echo '<input value="Đăng bài viết" type="button" class="btnsubmit" id="btnSubmitNewPost"/>';
    }

    ?>

    
    
    
</div>
</div> <!--end wrap-->
</div><!-- /shadowblock -->

</div><!-- /shadowblock_out -->

</div><!-- /content_left -->


<!-- right sidebar -->
<div class="content_right">
    <div class="shadowblock_out">
      <div class="shadowblock">
        <h2 class="dotted">Chọn danh mục</h2>
        <div class="recordfromblog">
            <ul id="list_category">
                <?php
                $args = array(
                    'type'                     => 'post',
                    'child_of'                 => 0,
                    'parent'                   => 0,
                    'orderby'                  => 'name',
                    'order'                    => 'ASC',
                    'hide_empty'               => 0,
                    'hierarchical'             => 1,
                    'exclude'                  => '',
                    'include'                  => '',
                    'number'                   => '',
                    'taxonomy'                 => 'category',
                    'pad_counts'               => false );

                    //$listCategory = getCategoryByUserId($current_user->ID);

                $listCategory = get_categories($args);

                foreach ($listCategory as $item) {
                            //  echo ' <li class="post_category" id="post_category_'.$item->term_id.'"><a href="javascript:void(0)">'.$item->name.'
                            // <input type="hidden" value="'.$item->term_id.'">
                            // </a>
                            // <a href="javascript:void(0);" title="Chỉnh sửa danh mục" class="edit_category"><img src="'.get_template_directory_uri().'/images/pencil.png" border="0"  alt="Chỉnh sửa danh mục"></a>
                            // <a href="javascript:void(0);" title="Xóa danh mục" class="edit_category"><img src="'.get_template_directory_uri().'/images/cross.png" border="0" alt="Xóa danh mục"></a>        
                            // </li>';
                   echo ' <li class="post_category" id="post_category_'.$item->term_id.'"><span>'.$item->name.'</span></li>';

               }

                    // echo '<li id="insert_new_category">
                    //         <input type="text" placeholder="Tạo thêm danh mục. Nhấn Enter để thực hiện" id="category_insert_new" style="width:100%;"></input>
                    //       </li>';
               ?>

                     <!--    <li><a href="">Bảng điều khiển</a></li>
                        <li><a href="http://yourstyle.name/raovat/?page_id=11">Thông tin cá nhân</a></li>
                        <li><a href="http://yourstyle.name/raovat/?page_id=11">Cài đặt</a></li> -->
                        <!-- <li><a href="http://yourstyle.name/raovat/wp-admin/">WordPress Admin</a></li> -->

                    </ul>
                </div><!-- /recordfromblog -->

            </div><!-- /shadowblock -->
        </div><!-- /shadowblock_out -->

    </div><!-- /content_right -->

    <div class="clr"></div>

</div><!-- /content_res -->

</div><!-- /content_botbg -->

</div>

<div id="wpbody">
    <div id="wpbody-content" style="overflow: hidden;" aria-label="Main content" tabindex="0">
    </div>
</div>

<?php get_footer();?>
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/includes/bootstrap/css/bootstrap.css" /> 